<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 分类ajax类
 * @package  	ajax
 * @subpackage  backend
 * @author   	create by allen.qiang
 */

quanli_require('module/base/goods/goodsCategory.class.php');
quanli_require('module/base/goods/goodsSpec.class.php');

class ajax_category {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch($_REQUEST['operFlg']) {

			    case 1: //添加商品分类
                    $this->saveCategory();
                break;
				case 2: //删除商品分类
					$this->deleteCategory();
				break;
				case 3: //修改商品分类使用状态
					$this->categoryUse();
				break;	
				case 4:	//修改排序
					$this->changeSort();
				break;
				case 5: //修改分类名称
					$this->changeName();
				break;
				case 6: //添加子类or添加分类or编辑
					$this->categoryLayer();
				break;
            }
        }
    }

   /**
    * 保存/编辑分类
    *
    */
    public function saveCategory () {
    	
    	global $G_SHOP;
    	$_REQUEST['creator'] = $G_SHOP->currentUserID;
    	$categoryHandler = new goodsCategory( $G_SHOP->DBCA ) ;
    	$result_array = $categoryHandler->saveCategory( $_REQUEST ) ;
    	echo json_encode($result_array);
    }
	
	public function deleteCategory () {
		global $G_SHOP;
		
    	$categoryHandler = new goodsCategory( $G_SHOP->DBCA ) ;
    	$result_array = $categoryHandler->deleteCategory( $_REQUEST ) ;
    	echo json_encode($result_array);
	}
	
	/**
	 * 修改商品分类使用状态
	 *
	 */
	public function categoryUse () {
		global $G_SHOP;
    	$categoryHandler = new goodsCategory( $G_SHOP->DBCA ) ;
    	$result_array = $categoryHandler->categoryUse( $_REQUEST ) ;
    	echo json_encode($result_array);
	}
	
	/**
	 * 修改排序
	 *
	 */
	public function changeSort(){
		global $G_SHOP;
    	$categoryHandler = new goodsCategory( $G_SHOP->DBCA ) ;
    	$result_array = $categoryHandler->changeSort( $_REQUEST ) ;
    	echo json_encode($result_array);
	}
	
	/**
	 * 修改分类名称
	 *
	 */
	public function changeName(){
		global $G_SHOP;
    	$categoryHandler = new goodsCategory( $G_SHOP->DBCA ) ;
    	$result_array = $categoryHandler->changeName( $_REQUEST ) ;
    	echo json_encode($result_array);
	}
	
	/**
	 * 分类添加编辑层
	 *
	 */
	public function categoryLayer(){
		
		global $G_SHOP;
		
		$specHandler = new goodsSpec($G_SHOP->DBCA);
		
		$categoryHandler = new goodsCategory($G_SHOP->DBCA);
		
		//规格组
		$arg_array = array();
		$specArray = $specHandler->goodsSpecGroupList($arg_array);

		//获取分类信息
		$category = $this->getCategory();
		
		//添加子类
		$parentID = isset($_REQUEST['parentID']) ? trim($_REQUEST['parentID']) : null;
		
		//编辑
		$level = isset($_REQUEST['levelID']) ? trim($_REQUEST['levelID']) : null;
		$categoryID = isset($_REQUEST['categoryID']) ? trim($_REQUEST['categoryID']) : null;
		$disabled = 0;
		$group = array();
		
		if (!empty($level) && !empty($categoryID)) {
			switch ($level) {
	        	case 1:	//大分类
	        		$categoryInfo = new entity('goods_bigcategory');
	        		$categoryInfo->bigCategoryID = $categoryID;
	        	break;
	        	case 2:	//中分类
	        		$categoryInfo = new entity('goods_midcategory');
	        		$categoryInfo->midCategoryID = $categoryID;
	        		
	        	break;
	        	case 3:	//小分类
	        		$categoryInfo = new entity('goods_smallcategory');
	        		$categoryInfo->smallCategoryID = $categoryID;
	        		
	        	break;
	        	default:
	        		die(json_encode(array('code' => 400, 'message' => '分类信息未找到')));
	        	break;	
	        }
	        
	        if (!$G_SHOP->DBCA->fetch($categoryInfo)) {
				die(json_encode(array('code' => 400, 'message' => '分类信息未找到')));
	        }
	        
			switch ($level) {
				case 1:	//大分类
					$categoryStr = $categoryInfo->bigCategoryID;
					$categoryInfo->id = $categoryID . '_' . 1;
					$categoryInfo->parentID = '0_0_1';
				break;
				case 2:	//中分类
					$categoryStr = $categoryInfo->bigCategoryID . $categoryInfo->midCategoryID;
					$categoryInfo->id = $categoryID . '_' . 2;
					$categoryInfo->parentID = $categoryInfo->bigCategoryID . '_0_2';
				break;
				case 3:	//小分类
					$categoryStr = $categoryInfo->bigCategoryID . $categoryInfo->midCategoryID . $categoryInfo->smallCategoryID;
					$categoryInfo->id = $categoryID . '_' . 3;
					$categoryInfo->parentID = $categoryInfo->bigCategoryID . '_' . $categoryInfo->midCategoryID . '_3';
				break;
			}
	         
	        //已选规格组
	        $arg_array = array(
	        	'categoryID' => $categoryStr
	        );
	        $selectedSpec = $categoryHandler->categoryMappingSpec($arg_array);
	        
	        if ($selectedSpec) {
	        	foreach ($selectedSpec as &$spec) {
	        		$group[] = $spec['specGroupID'];
	        	}
	        }
	        
	        $disabled = 1;
	        $pitch = $categoryInfo->parentID;
	        $G_SHOP->smarty->assign('categoryInfo' , $categoryInfo);
		} else {
			$pitch = $parentID;
			$G_SHOP->smarty->assign('parentID' , $parentID);
		}
		
		$G_SHOP->smarty->assign('category' , $category);
		$G_SHOP->smarty->assign('specArray' , $specArray);
		$G_SHOP->smarty->assign('pitch' , $pitch);
		$G_SHOP->smarty->assign('disabled' , $disabled);
		$category_layer = $G_SHOP->smarty->fetch('goods/categoryLayer.tpl');

		echo json_encode(
			array(
				'code' => 200, 
				'message' => '',
				'disabled' => $disabled,
				'pitch' => $pitch,
				'data' => $category_layer,
				'group' => $group
			)
		);
	}
	
	/**
     * 获取所有分类信息
     *
     */
   	public function getCategory(){
   		
   		global $G_SHOP;

        $categoryHandler = new goodsCategory( $G_SHOP->DBCA ) ;
        
        //查询大分类
        $big_array = array(
        	'sortname' => 'sortOrder', 
        	'sortorder' => 'ASC'
        );
        $category = $categoryHandler->searchBigCategory( $big_array ) ;
        
        //查询中分类
        if ($category) {
        	foreach ($category as $b=>$bigVal) {
        		$mid_array = array(
        			'bigCategoryID' => $bigVal['bigCategoryID'],
        			'sortname'      => 'sortOrder', 
        			'sortorder'     => 'ASC'
        		);
        		$mid_category = $categoryHandler->searchMidCategory( $mid_array ) ;
        		
        		if ($mid_category) {
        			foreach ($mid_category as $m=>$midVal) {
        				$small_array = array(
        					'bigCategoryID' => $bigVal['bigCategoryID'],
        					'midCategoryID' => $midVal['midCategoryID'],
		        			'sortname'      => 'sortOrder', 
		        			'sortorder'     => 'ASC'
        				);
        				$small_category = $categoryHandler->searchSmallCategory( $small_array ) ;
        				
        				$mid_category[$m]['smallCategory'] = $small_category;
        			}
        		}
        		$category[$b]['midCategory'] = $mid_category;
        	}
        }
        
        return $category;
   	}
}
